<?php

namespace App\Http\Controllers\Admin\RoomManager;

use Carbon\Carbon;
use App\Enums\RoomEnum;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Response\Response;

class RoomBannerController extends BaseController
{

    /**
     * @api {get} /room/banner 房间轮播图-列表
     * @apiVersion 0.1.0
     * @apiName 房间轮播图-列表
     * @apiGroup 房间管理
     *
     * @apiParam {string} name 名称,非必传
     * @apiParam {number} status 状态1显示0隐藏,非必传
     *
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     */
    public function list(Request $request)
    {
        $name = $request->input('name');
        $status = $request->input('status', -1);
        $where = [];
        if ($name) {
            $where[] = ['name', 'like', "%$name%"];
        }
        if (in_array($status, [0, 1])) {
            $where[] = ['status', $status];
        }
        $list = app('model_room_banner')->where($where)->orderBy('sort', 'desc')->orderBy('id', 'desc')->get()->toArray();
        foreach ($list as &$value) {
            $value['start_time'] = $value['start_time'] > 0 ? date('Y-m-d H:i:s', $value['start_time']) : '';
            $value['end_time'] = $value['end_time'] > 0 ? date('Y-m-d H:i:s', $value['end_time']) : '';
            $value['type_text'] = RoomEnum::ROOM_BANNER_OPERATE[$value['type']] ?? '';
            if ($value['allow_room_type']) {
                $value['allow_room_type_text'] = app('model_room_type')->whereIn('id', explode(',', $value['allow_room_type']))->pluck('room_type_name')->implode(',');
            } else {
                $value['allow_room_type_text'] = '';
            }
            $value['time_slot_text'] = $this->splitTime($value['time_slot']);
            $value['time_show_text'] = $this->splitTime($value['time_show']);

        }

        return Response::sendData($list);
    }

    //分割时间段
    public function splitTime($time)
    {
        $timeData = explode(',', $time);
        $time_text = '';
        if (is_array($timeData) && $time) {
            foreach ($timeData as $t) {
                $array = explode('-', $t);
                $begin = $array[0] >= 0 ? $array[0] : -1;
                $end = $array[1] >= 0 ? $array[1] : -1;
                if ($begin >= 0 && $end >= 0) {
                    $time_text .= "$begin:00-$end:00<br>";
                }
            }
        }

        return $time_text;
    }

    /**
     * @api {post} /room/banner 房间轮播图-添加
     * @apiVersion 0.1.0
     * @apiName 房间轮播图-添加
     * @apiGroup 房间管理
     *
     * @apiParam {string} name 挂件名称,必传
     * @apiParam {number} status 状态1显示0隐藏默认0,非必传
     * @apiParam {string} start_time 上线时间格式如2021-07-01 16:00:00,非必传
     * @apiParam {string} end_time 关闭时间,非必传
     * @apiParam {string} picture 挂件图标,非必传
     * @apiParam {number} type 操作类型1小猫钓鱼,2球球大作战,3跳转链接,4boss大作战,6幸运树游戏,7抢糖果游戏,非必传默认3
     * @apiParam {string} url 链接,非必传
     * @apiParam {number} position 显示位置固定值为1,2,3,非必传默认1
     * @apiParam {number} sort 排序,非必传默认0
     * @apiParam {string} allow_user 只允许显示的用户,默认空,全部允许,非必传,多个用","隔开
     * @apiParam {string} allow_room 只允许显示的房间,默认空,全部允许,非必传,多个用","隔开
     * @apiParam {string} allow_room_type 只允许显示的房间类型,默认空,全部允许,非必传,多个用","隔开
     * @apiParam {number} allow_rank 只允许显示的用户等级,默认0,全部允许,非必传
     * @apiParam {string} time_slot 允许显示的时间段,默认空,全部允许,非必传,格式如:1-4,5-24
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     */
    public function addRoomBanner(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                "name"     => "required|string|max:255",
                "status"   => "filled|numeric|in:0,1",
                "picture"  => "required",
                "position" => "required|numeric|in:1,2,3",
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, getValidateMessage($v->messages()));
        }
        $data = [
            'name' => $request->input('name'),
            'picture' => $request->input('picture'),
            'type' => $request->input('type', 3),
            'url' => $request->input('url'),
            'position' => $request->input('position', 1),
            'start_time' => $request->input('start_time'),
            'end_time' => $request->input('end_time', 0),
            'allow_user' => $request->input('allow_user'),
            'allow_room' => $request->input('allow_room'),
            'allow_room_type' => $request->input('allow_room_type'),
            'allow_rank' => $request->input('allow_rank', 0),
            'sort' => $request->input('sort', 0),
            'status' => $request->input('status', 0),
            'time_slot' => $request->input('time_slot', ''),
            'time_show' => $request->input('time_show', ''),
        ];
        $now_time = Carbon::now()->timestamp;
        $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : $now_time;
        $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
        $data['add_time'] = $now_time;
        app('model_room_banner')->insert($data);
        app('redis')->del('room:banner');
        app('redis')->del('room:config:banner');

        return Response::sendData(true);
    }
    /**
     * @api {post} /room/banner/update 房间轮播图-修改
     * @apiVersion 0.1.0
     * @apiName 房间轮播图-修改
     * @apiGroup 房间管理
     *
     * @apiParam {number} id 主键id,必传
     * @apiParam {string} name 挂件名称,非必传
     * @apiParam {number} status 状态1显示0隐藏默认0,非必传
     * @apiParam {string} start_time 上线时间格式如2021-07-01 16:00:00,非必传
     * @apiParam {string} end_time 关闭时间,非必传
     * @apiParam {string} picture 挂件图标,非必传
     * @apiParam {number} type 操作类型1小猫钓鱼,2球球大作战,3跳转链接,4boss大作战,6幸运树游戏,7抢糖果游戏,非必传默认3
     * @apiParam {string} url 链接,非必传
     * @apiParam {number} position 显示位置固定值为1,2,3,非必传默认1
     * @apiParam {number} sort 排序,非必传默认0
     * @apiParam {string} allow_user 只允许显示的用户,默认空,全部允许,非必传,多个用","隔开
     * @apiParam {string} allow_room 只允许显示的房间,默认空,全部允许,非必传,多个用","隔开
     * @apiParam {string} allow_room_type 只允许显示的房间类型,默认空,全部允许,非必传,多个用","隔开
     * @apiParam {number} allow_rank 只允许显示的用户等级,默认0,全部允许,非必传
     * @apiParam {string} time_slot 允许显示的时间段,默认空,全部允许,非必传,格式如:1-4,5-24
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     */
    public function updateRoomBanner(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                "id"     => "required",
                "name"     => "filled|string|max:255",
                "status"   => "filled|numeric|in:0,1",
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, getValidateMessage($v->messages()));
        }
        $data = [
            'name' => $request->input('name'),
            'picture' => $request->input('picture'),
            'type' => $request->input('type', 3),
            'url' => $request->input('url'),
            'position' => $request->input('position', 1),
            'start_time' => $request->input('start_time'),
            'end_time' => $request->input('end_time', 0),
            'allow_user' => $request->input('allow_user'),
            'allow_room' => $request->input('allow_room'),
            'allow_room_type' => $request->input('allow_room_type'),
            'allow_rank' => $request->input('allow_rank', 0),
            'sort' => $request->input('sort', 0),
            'status' => $request->input('status', 0),
            'time_slot' => $request->input('time_slot', ''),
            'time_show' => $request->input('time_show', ''),
        ];
        $id = $request->input('id', 0);
        $now_time = Carbon::now()->timestamp;
        $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : $now_time;
        $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
        $data['update_time'] = $now_time;
        app('model_room_banner')->where('id', $id)->update($data);
        app('redis')->del('room:banner');
        app('redis')->del('room:config:banner');

        return Response::sendData(true);
    }
    /**
     * @api {delete} /room/banner 房间轮播图-删除
     * @apiVersion 0.1.0
     * @apiName 房间轮播图-删除
     * @apiGroup 房间管理
     *
     * @apiParam {number} id 主键id,必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     */
    public function deleteRoomBanner(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                "id"     => "required",
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, getValidateMessage($v->messages()));
        }
        $id = $request->input('id', 0);
        app('model_room_banner')->where('id', $id)->delete();

        app('redis')->del('room:banner');
        app('redis')->del('room:config:banner');

        return Response::sendData(true);
    }
}
